<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\TvmMobileDetail;
use App\Models\Admin\TvmMobileCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\AdminAction;

class MobileController extends CommonController {

	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}

	/**
	 * @param Request $request
	 * 手机分类信息表
	 */
	public function category(Request $request){
		$condition = array();
		$keywords = $request->input('keywords');
		if($keywords){
			$condition[] = ['name', 'like', '%'.$keywords.'%'];
		}
		$list = Db::table('tvm_mobile_category')->where($condition)->orderBy('create_time','desc')->paginate(5);
		return view('admin.mobile.category',[
			'data' => $list,
			'keywords' => $keywords
		]);
	}

	/**
	 * @param Request $request
	 * 手机分类信息添加
	 */
	public function category_add(Request $request){
		if($request->isMethod('post')){
			$name = $request->input('name');
			if(!$name){
				$this->ajax_error('分类名称不能为空');
			}
			$data = array(
				'id' => TvmMobileCategory::getNextSeq(),
				'parent_id' => $request->input('parent_id'), //父级ID
				'name' => $request->input('name'), //名称
				'status' => $request->input('status'), //状态
				'remarks' => $request->input('remarks') ?: '', //备注
			);
			$result = Db::table('tvm_mobile_category')->insert($data);
			if(!$result){
				$this->ajax_error('添加失败');
			}
			record_action(AdminAction::getNextSeq(),'Mobile','category_add','手机分类信息添加',json_encode($_POST));
			$this->ajax_success('添加成功');
		}

		$category = DB::table('tvm_mobile_category')->select('id','parent_id','name')->get();
		return view('admin.mobile.category_edit',[
			'category' => $category,
			'action' => 'add'
		]);
	}

	/**
	 * @param Request $request
	 * 手机分类信息编辑
	 */
	public function category_edit(Request $request){
		if($request->isMethod('post')){
			$data = array();
			$id = $request->input('id');
			if($name = $request->input('name')){
				$data['name'] = $name;
			}
			if($remarks = $request->input('remarks')){
				$data['remarks'] = $remarks;
			}
			$data['parent_id'] = $request->input('parent_id');
			$data['status'] = $request->input('status');
			$result = DB::table('tvm_mobile_category')->where('id',$id)->update($data);
			$this->ajax_error('添加失败',$data);
			if(!$result){
				$this->ajax_error('修改失败');
			}
			record_action(AdminAction::getNextSeq(),'Mobile','category_edit','手机分类信息编辑',json_encode($_POST));
			$this->ajax_success('修改成功');
		}
		$id = $request->input('id');
		$data = DB::table('tvm_mobile_category')->where('id',$id)->first();
		$category = DB::table('tvm_mobile_category')->select('id','parent_id','name')->get();
		return view('admin.mobile.category_edit',[
			'category' => $category,
			'id' => $id,
			'data' => $data,
			'action' => 'edit'
		]);
	}


	/**
	 * @param Request $request
	 * 手机型号详情展示页
	 */
	public function index(Request $request){
		$condition = array();
		$cate_id = $request->input('cate_id');
		if($cate_id){
			$condition[] = ['cate_id','=',$cate_id];
		}
		$keywords = $request->input('keywords');
		if($keywords){
			$condition[] = ['title', 'like', '%'.$keywords.'%'];
		}
		$list = DB::table('tvm_mobile_detail')->where($condition)->orderBy('create_time','desc')->paginate(5);
		$category = DB::table('tvm_mobile_category')->select('id','parent_id','name')->get();
		return view('admin.mobile.index',[
			'data' => $list,
			'category' => $category,
			'keywords' => $keywords
		]);
	}

	/**
	 * @param Request $request
	 * 手机型号添加
	 */
	public function add(Request $request){
		if($request->isMethod('post')){
			$cate_id = $request->input('cate_id');
			if(!$cate_id){
				$this->ajax_error('CATE_ID分类为空');
			}
			$title = $request->input('title');
			if(!$title){
				$this->ajax_error('手机型号不能为空');
			}
			$pic = $request->input('pic');
			if(!$pic){
				$this->ajax_error('预览图不能为空');
			}
			$color = $request->input('color');
			if(!$color){
				$this->ajax_error('颜色不能为空');
			}
			$color = str_replace(array('，',' ','、'),array(',',',',','),$color); //中文逗号\空格\顿号 替换
			$colorJson = json_encode(explode(',',$color));
			$data = array(
				'id' => TvmMobileDetail::getNextSeq(),
				'cate_id' => $cate_id,
				'pic'		=> $pic,
				'title' => $title,
				'cost_price' => $request->input('cost_price'), //成本价
				'market_price' => $request->input('market_price'), //市场价
				'sell_price' => $request->input('sell_price'), //销售价
				'return_num' => $request->input('return_num'), //分期数
				'pv' => $request->input('pv'),
				'details' => $request->input('details'), //商品详情
				'store_num' => $request->input('store_num'), //库存
				'base_task' => $request->input('base_task'), //基础任务
				'rom' => $request->input('rom'), //手机内存
				'cpu' => $request->input('cpu'),
				'camera' => $request->input('camera'), //像素
				'screen' => $request->input('screen'), //屏幕尺寸
				'net' => $request->input('net'), //网路制式
				'color' => $colorJson,
				'remarks' => $request->input('remarks'),
				'resolution' => $request->input('resolution') //分辨率
			);
			$data = array_filter($data);
			filedebug('添加数据','./log/mobile_add.php');
			filedebug($data,'./log/mobile_add.php');
			$result = DB::table('tvm_mobile_detail')->insert($data);
			filedebug('添加结果','./log/mobile_add.php');
			filedebug($result,'./log/mobile_add.php');
			if(!$result){
				$this->ajax_error('添加失败');
			}
			record_action(AdminAction::getNextSeq(),'Mobile','add','手机型号添加',json_encode($_POST));
			$this->ajax_success('添加成功');
		}
		//add页面category分类
		$category = DB::table('tvm_mobile_category')->select('id','parent_id','name')->get();
		return view('admin.mobile.add',[
			'category'  => $category,
			'action'	=> 'add',
		]);
	}

	/**
	 * @param Request $request
	 * 手机型号编辑
	 */
	public function edit(Request $request){
		if($request->isMethod('post')){
			$data = array();
			$id = $request->input('id');
			if(!$id){
				$this->ajax_error('ID为空');
			}
			if($cate_id = $request->input('cate_id')){
				$data['cate_id'] = $cate_id;
			}else{
				$this->ajax_error('CATE_ID分类为空');
			}
			if($title = $request->input('title')){
				$data['title'] = $title;
			}else{
				$this->ajax_error('手机型号不能为空');
			}
			if($pic = $request->input('pic')){
				$data['pic'] = $pic;
			}else{
				$this->ajax_error('预览图不能为空');
			}
			if($pv = $request->input('pv')){
				$data['pv'] = $pv;
			}
			if($cost_price = $request->input('cost_price')){ //成本价
				$data['cost_price'] = $cost_price;
			}
			if($mark_price = $request->input('mark_price')){ //市场价
				$data['mark_price'] = $mark_price;
			}
			if($sell_price = $request->input('sell_price')){  //销售价
				$data['sell_price'] = $sell_price;
			}
			if($return_num = $request->input('return_num')){ //分期数量
				$data['return_num'] = $return_num;
			}
			if($store_num = $request->input('store_num')){ //库存
				$data['store_num'] = $store_num;
			}
			if($base_task = $request->input('base_task')){ //基础任务
				$data['base_task'] = $base_task;
			}
			if($remarks = $request->input('remarks')){ //备注
				$data['remarks'] = $remarks;
			}
			if($rom = $request->input('rom')){ //手机内存
				$data['rom'] = $rom;
			}
			if($cpu = $request->input('cpu')){
				$data['cpu'] = $cpu;
			}
			if($camera = $request->input('camera')){ //摄像头
				$data['camera'] = $camera;
			}
			if($screen = $request->input('screen')){ //屏幕尺寸
				$data['screen'] = $screen;
			}
			if($net = $request->input('net')){ //网络制式
				$data['net'] = $net;
			}
			if($color = $request->input('color')){ //颜色
				$color = str_replace(array('，',' ','、'),array(',',',',','),$color); //中文逗号\空格\顿号 替换
				$data['color'] = json_encode(explode(',',$color));
			}
			if($resolution = $request->input('resolution')){ //分辨率
				$data['resolution'] = $resolution;
			}
			if($details = $request->input('details')){ //详情
				$data['details'] = $details;
			}
			$data['update_time'] = date('Y-m-d H:i:s',time()); //更新时间
			filedebug('修改数据','./log/mobile_edit.php');
			filedebug($data,'./log/mobile_edit.php');
			$result = DB::table('tvm_mobile_detail')->where('id',$id)->update($data);
			filedebug('修改结果','./log/mobile_edit.php');
			filedebug($result,'./log/mobile_edit.php');
			if(!$result){
				$this->ajax_error('修改失败');
			}
			record_action(AdminAction::getNextSeq(),'Mobile','edit','手机型号编辑',json_encode($_POST));
			$this->ajax_success('修改成功');
		}

		$id = $request->input('id');
		//编辑页面信息
		$productInfo = DB::table('tvm_mobile_detail')->where('id',$id)->first(); //产品信息
		$categoryInfo = DB::table('tvm_mobile_category')->where('id',$productInfo->cate_id)->first(); //商品栏目信息
		$ArrayColor = json_decode($productInfo->color,true);
		if($ArrayColor){
			$productInfo->color = implode(',',$ArrayColor); //将json转为字符
		}
		return view('admin.mobile.add',[
			'data' => $productInfo,
			'categoryInfo' => $categoryInfo,
			'action' => 'edit'
		]);
	}

	public function agent(Request $request){
		$condition = array();
		$name = $request->input('name');
		if($name){
			$condition[] = ['name', 'like', '%'.$name.'%'];
		}
		$list = DB::table('tvm_agent')->where($condition)->paginate(5);
		foreach ($list as $k => $v){
			$list[$k]->nodename = DB::table('tnet_reginfo')->where(['nodeid' => $v->node_id])->first()->nodename ?: '';
		}
		return view('admin.mobile.agent',[
			'data' => $list,
			'name' => $name
		]);
	}

	public function agent_edit(Request $request){
		$id = $request->input('id');
		if($request->isMethod('post')){
			$data = array();
			if(!$id){
				$this->ajax_error('ID为空');
			}
			if($name = $request->input('name')){
				$data['name'] = $name;
			}
			if($address = $request->input('address')){
				$data['address'] = $address;
			}
			$result = DB::table('tvm_agent')->where('id',$id)->update($data);
			if(!$result){
				$this->ajax_error('修改失败');
			}
			record_action(AdminAction::getNextSeq(),'Mobile','agent_edit','手机区域代理修改',json_encode($_POST));
			$this->ajax_success('修改成功');
		}
		$agentInfo = Db::table('tvm_agent')->where('id',$id)->first();
		$agentInfo->nodename = DB::table('tnet_reginfo')->where(['nodeid' => $agentInfo->node_id])->first()->nodename;
		$agentInfo->region_name = DB::table('tvip_project')->where(['id' => $agentInfo->region_id])->first()->name;
		return view('admin.mobile.agent_edit',[
			'data' => $agentInfo,
			'action' => 'edit'
		]);
	}

	/**
	 * @param Request $request
	 * 删除手机型号数据
	 */
	public function del(Request $request){
		$id = $request->input('id');
		$result = DB::table('tvm_mobile_detail')->where('id',$id)->delete();
		if($result){
			record_action(AdminAction::getNextSeq(),'Mobile','del','删除手机型号数据',json_encode($_POST));
			$this->ajax_success('删除成功',$result);
		}
		$this->ajax_error('删除失败',$result);
	}


}

